#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        //child
        printf("我是子进程\n");
        char* const _argv[] = {
           (char*) "ls",
           (char*) "-a",
           (char*) "-l",
           (char*) "--color=auto",
            NULL
        };
        char* const _env[] = {
           (char*) "MY_ENV_VAL=123456",
           NULL
        };

        execve("./test", _argv, _env);
        //execle("/home/kele/linux/4_9/test", "test", NULL, _env);
        //execvp("ls", _argv);
        //execv("/usr/bin/ls", _argv);
        //execlp("ls", "ls", "-a", "-l", "--color=auto", NULL);
        //execl("/usr/bin/ls", "ls", "-a", "-l", "--color=auto", NULL);
        exit(2);
    }
    else
    {
        int status = 0;
        pid_t ret = waitpid(id, &status, 0);
        if(ret > 0)
        {
            if(WIFEXITED(status))
            {
                printf("子进程返回码：%d\n",WEXITSTATUS(status));
            }
            else if(WIFSIGNALED(status))
            {
                printf("子进程终止信号：%d\n", WTERMSIG(status));
            }
        }
        else
        {
            exit(1);
        }
    }
    return 0;
}
